<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改考评评语' : '新增考评评语'"
    width="800px"
    :footer="null"
    :visible.sync="visible"
  >
    <el-form :model="form" ref="form" label-width="120px" :rules="rules">
      <el-form-item label="评语模板名称" prop="commentTemplateName">
        <el-input v-model="form.commentTemplateName" placeholder="请输入评语模板名称" />
      </el-form-item>
      <!-- <el-form-item label="考评类型"></el-form-item> -->
      <el-form-item label="考评环节" prop="moralityStep">
        <el-select clearable v-model="form.moralityStep" style="width: 100%">
          <el-option :value="1" label="自评" />
          <el-option :value="2" label="科室" />
          <el-option :value="3" label="党支部" />
          <el-option :value="4" label="考评委员会" />
        </el-select>
      </el-form-item>
      <el-form-item label="评语" prop="commentContent">
        <el-input type="textarea" :rows="3" v-model="form.commentContent" placeholder="请输入评语内容" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSure">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { templateCreate, templateUpdate } from '@/api/morality'
import { openNotification } from '@/utils/util'

export default {
  data() {
    return {
      isEdit: false,
      visible: false,
      form: {
        commentTemplateName: null,
        moralityStep: null,
        commentContent: null
      },
      rules: {
        commentTemplateName: [{ required: true, message: '请输入评语模板名称', trigger: 'blur' }],
        moralityStep: [{ required: true, message: '请选择考评环节', trigger: 'blur' }],
        commentContent: [{ required: true, message: '请输入评语内容', trigger: 'blur' }]
      }
    }
  },
  methods: {
    edit(bool, row) {
      this.isEdit = bool
      this.form = Object.assign({}, row)
      this.visible = true
    },
    handleSure() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = '',
            mes = ''
          if (this.isEdit) {
            mes = '修改操作成功'
            res = await templateUpdate(this.form)
          } else {
            mes = '新增操作成功'
            res = await templateCreate(this.form)
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>
